<?php

namespace App\Http\Controllers;

use App\Models\Member;
use App\Models\Red;
use App\Models\Setting;
use Illuminate\Http\Request;
use Kuyuan\Phpwidget\Str;

class HomeController extends Controller
{
    /**
     * 首页系统数据
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
     */
    public function getInfo()
    {
        $_data = array();
        // 用户总数
        $_data[] = Member::count();
        // 今日新增用户数量
        $_data[] = $this->getDayMember();
        // 红包总数
        $_data[] = Red::where("status","!=",1)
            ->where("status","!=",6)
            ->where("status","!=",8)
            ->count();
        // 今日红包数量
        $_data[] = $this->getDayRedNumber();
        // 获取系统总收入
        $_data[] = Str::format_money(Red::where("status","!=",1)
            ->where("status","!=",6)
            ->where("status","!=",8)
            ->sum("charge")).'元';
        // 获取今日系统收入
        $_data[] = Str::format_money($this->getDayIncome()).'元';
        return response(['data'=>$_data]);
    }

    /**
     * 获取月数据
     */
    public function getMonth()
    {
        $_month = strtotime(date('Y-m'));
        $_end_month = strtotime('2021-11');
        // 每月的开始时间戳和结束时间戳
        $_months = array();
        do{
            $_temp_date = array(
                'start' => $_month,
                'end' => $_month + date('t',$_month) * 86400 -1
            );
            $_month = strtotime('-1 month',$_month);
            $_months[] = $_temp_date;
        }while($_month >= $_end_month);
        $_data = array();
        foreach($_months as $_k=>$_v) {
            $_data[] = array(
                'date' => date('Y年m月',$_v['start']),
                'number' => Red::where("status","!=",1)
                    ->where("status","!=",6)
                    ->where("status","!=",8)
                    ->whereRaw("created_at >= ?",[date('Y-m-d',$_v['start'])])
                    ->whereRaw("created_at < ?",[date('Y-m-d H:i:s',$_v['end'])])
                    ->count(),
                'money' => Str::format_money(Red::where("status","!=",1)
                    ->where("status","!=",6)
                    ->where("status","!=",8)
                    ->whereRaw("created_at >= ?",[date('Y-m-d',$_v['start'])])
                    ->whereRaw("created_at < ?",[date('Y-m-d H:i:s',$_v['end'])])
                    ->sum('pay'))
            );
        }
        return response(['data'=>$_data]);
    }

    /**
     * 后台首页统计表数据
     */
    public function getStatisticsDate(Request $request)
    {
        $_data = array();
       // $_data = Member::selectRaw('UNIX_TIMESTAMP(created_at)')->first();
        foreach ($request->date as $_v) {
            $_data["member"][] = $this->getDayMember($_v);
            $_data["red_number"][] = $this->getDayRedNumber($_v);
            $_data["income"][] = Str::format_money($this->getDayIncome($_v));
        }
        return response(['data'=>$_data]);
    }

    /**
     * 获取指定日期新增的用户
     * @param string $_date 日期 格式 2020-10-20
     * @return mixed
     */
    private function getDayMember($_date = "")
    {
        if(empty($_date)) {
            $_start_time = date("Y-m-d");
            $_end_time = date("Y-m-d",time()+86400);
        } else {
            $_start_time = $_date;
            $_end_time = date('Y-m-d',strtotime($_date) + 86400);
        }
        return Member::whereRaw("created_at >= ?",[$_start_time])
            ->whereRaw("created_at < ?",[$_end_time])
            ->count();
    }

    /**
     * 获取指定日期的红包数量
     * @param string $_date 日期 格式 2020-10-29
     * @return mixed
     */
    private function getDayRedNumber($_date = "")
    {
        if(empty($_date)) {
            $_start_time = date("Y-m-d");
            $_end_time = date("Y-m-d",time()+86400);
        } else {
            $_start_time = $_date;
            $_end_time = date('Y-m-d',strtotime($_date) + 86400);
        }
        return Red::where("status","!=",1)
            ->where("status","!=",6)
            ->where("status","!=",8)
            ->whereRaw("created_at >= ?",[$_start_time])
            ->whereRaw("created_at < ?",[$_end_time])
            ->count();
    }

    /**
     * 获取系统指定日期的收入
     * @param string $_date 日期 格式2020-10-23
     * @return mixed
     */
    private function getDayIncome($_date = "")
    {
        if(empty($_date)) {
            $_start_time = date("Y-m-d");
            $_end_time = date("Y-m-d",time()+86400);
        } else {
            $_start_time = $_date;
            $_end_time = date('Y-m-d',strtotime($_date) + 86400);
        }
        return Red::where("status","!=",1)
            ->where("status","!=",6)
            ->where("status","!=",8)
            ->whereRaw("created_at >= ?",[$_start_time])
            ->whereRaw("created_at < ?",[$_end_time])
            ->sum("charge");
    }

    /**
     * 获取备案信息
     */
    public function bei()
    {
        $Setting = Setting::select('aq','ga')->first();
        return response(['data'=>$Setting]);
    }
}
